package org.jeecg.common.aspect.annotation;

import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import java.lang.reflect.Field;
import java.time.LocalTime;

/**
 * @author gbstart
 * @create 2024-07-13 9:24
 */
public class LogtimeEntityListener {

    @PrePersist
    public void prePersist(Object entity) {
        try {
            Field field = entity.getClass().getDeclaredField("logtime");
            field.setAccessible(true);
            field.set(entity,String.valueOf(System.currentTimeMillis() + LocalTime.now().getNano()));
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
//        entity.setLogtime(LocalDateTime.now());
    }

    @PreUpdate
    public void preUpdate(Object entity) {
        Field field = null;
        try {
            field = entity.getClass().getDeclaredField("logtime");
            field.setAccessible(true);
            field.set(entity,String.valueOf(System.currentTimeMillis() + LocalTime.now().getNano()));
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }
}
